package com.example.hasee.plant.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.plant.R;
import com.example.hasee.plant.config.MyApplication;
import com.example.hasee.plant.utils.MakeAudioByAUDIO;
import com.example.hasee.plant.utils.PlayAudioUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by jyb on 2017/2/2.
 */

public class GatherRecordFragment extends Fragment {
    private View view;
    private ImageButton iv_record;
    private TextView tv_play;
    private MakeAudioByAUDIO audio;
    private String recordPath="";
    PlayAudioUtils playAudioUtils;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.gather_record_frag,null);
        iv_record= (ImageButton) view.findViewById(R.id.iv_recording);
        tv_play= (TextView) view.findViewById(R.id.tv_playRecord);
        audio=new MakeAudioByAUDIO(MyApplication.GATHER_NUM);
        onListener();
        return view;
    }

    private void onListener(){
        iv_record.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if(MyApplication.AUDIO_PATH_FINISH!=null){
                            File file=new File(MyApplication.AUDIO_PATH_FINISH);
                            if(file.exists()){
                                file.delete();
                            }
                        }
                        Toast.makeText(getActivity(),"录音中...",Toast.LENGTH_SHORT).show();
                        iv_record.setImageResource(R.mipmap.records);

//                        try {
//                            Toast.makeText(getActivity(),"down",Toast.LENGTH_SHORT).show();
//                            audio.startRecording();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    audio.startRecording();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                        break;
                    case MotionEvent.ACTION_UP:
                        Toast.makeText(getActivity(),"STOP...",Toast.LENGTH_SHORT).show();
                        iv_record.setImageResource(R.mipmap.record);
                        audio.isRecording=false;
                        MyApplication.AUDIO_PATH_FINISH=audio.path;
                        break;
                }
                return false;
            }
        });

        tv_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    playAudioUtils=new PlayAudioUtils(audio.path);
                    playAudioUtils.play();
                } catch (Exception e) {
                    Toast.makeText(getActivity(),"找不到录音文件",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });

    }
}
